/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.dialog;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.factories.ButtonBarFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;
import org.jdesktop.swingx.renderer.ListCellContext;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.api.renderers.SubstanceDefaultListCellRenderer;
import org.jvnet.substance.skin.SkinInfo;
import tnviet.proj.jna.JNMainFrame;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.utilities.ResourceManager;


/**
 *
 * @author Administrator
 */
public class ConfigDialog  extends JDialog{
    JButton appearanceButton, tableViewButton, okButton, cancelButton;
    JComboBox lookAndFeelCombobox, languageCombobox;
    ResourceManager resourceManager = new ResourceManager(this.getClass(), Program.locale);
    JCheckBox useDecoratedChk, miniToTray,runAtStartup, notShutdown;

    public ConfigDialog(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        this.createLookAndFeelCombobox();
        this.createLanguageCombobox();
        FormLayout layout = new FormLayout("pref:grow,3dlu, pref", "pref, pref");
        CellConstraints cc = new CellConstraints();
        PanelBuilder builder = new PanelBuilder(layout);
        builder.add(this.createGeneralPane(), cc.xy(1, 1));
        builder.add(this.createShortcutsPane(), cc.xy(3, 1));
        builder.add(this.createButtonBar(), cc.xyw(1, 2,3));
        builder.setDefaultDialogBorder();
        this.getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
        this.pack();
    }


    public void createLookAndFeelCombobox(){
        Vector lafVector = new Vector();
        int currentValue = 0;
        String oldlaf = Program.JNProperty.getProperty("config.lookAndFeel");
        UIManager.LookAndFeelInfo lookAndFeelInfos[] = UIManager.getInstalledLookAndFeels();
        for(UIManager.LookAndFeelInfo lafInfo: lookAndFeelInfos) {
            lafVector.add(lafInfo);
            if(oldlaf.equals(lafInfo.getClassName())) currentValue = lafVector.size()-1;
        }
        for (SkinInfo skinInfo : SubstanceLookAndFeel.getAllSkins().values()) {
            lafVector.add(skinInfo);
            if(oldlaf.equals(skinInfo.getClassName())) currentValue = lafVector.size()-1;
        }
        DefaultComboBoxModel lafModel = new DefaultComboBoxModel(lafVector);
        lookAndFeelCombobox = new JComboBox(lafModel);
        lookAndFeelCombobox.setRenderer(new LookAndFeelComboboxRenderer());
        lookAndFeelCombobox.setSelectedIndex(currentValue);
    }



    public void createLanguageCombobox(){
        int languageCount = Integer.parseInt(resourceManager.getString("languageCount"));
        Vector langVector = new Vector();
        for(int i=0; i<languageCount; i++){
            langVector.add(i+1);
        }
        DefaultComboBoxModel langModel = new DefaultComboBoxModel(langVector);
        languageCombobox = new JComboBox(langModel);
        languageCombobox.setRenderer(new LanguageComboboxRenderer());
        String lang = Program.JNProperty.getProperty("config.language");
        for(int i = 0; i<languageCombobox.getItemCount(); i++){
            int languageIndex = (Integer)languageCombobox.getItemAt(i);
            String tempLang  = resourceManager.getString("language" + languageIndex + ".lang");
            if(lang.equals(tempLang)){
                System.out.println("aaaaa" + i);
                languageCombobox.setSelectedIndex(i);
                break;
            }
        }
    }
    private JPanel createButtonBar(){
        JPanel buttonPane;
        okButton = new JButton(resourceManager.getString("ok"));
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object lookAndFeel = lookAndFeelCombobox.getSelectedItem();
                if(lookAndFeel instanceof UIManager.LookAndFeelInfo){
                    JNMainFrame.updateLookAndFeel();
                    Program.JNProperty.setProperty("config.lookAndFeel", ((LookAndFeelInfo)lookAndFeel).getClassName());
                } else if(lookAndFeel instanceof SkinInfo){
                    JNMainFrame.updateLookAndFeel();
                    Program.JNProperty.setProperty("config.lookAndFeel", ((SkinInfo)lookAndFeel).getClassName());
                }
                int languageIndex = (Integer)languageCombobox.getSelectedItem();
                String language = resourceManager.getString("language" + languageIndex + ".lang");
                String country = resourceManager.getString("language" + languageIndex + ".country");
                Program.JNProperty.setProperty("config.country", country);
                Program.JNProperty.setProperty("config.language", language);
                boolean ret = useDecoratedChk.isSelected();
                Program.JNProperty.setProperty("config.isDefaultDecorated", Boolean.toString(ret));
                ret = miniToTray.isSelected();
                Program.JNProperty.setProperty("config.miniToTray", Boolean.toString(ret));
                ret = runAtStartup.isSelected();
                Program.JNProperty.setProperty("config.runAtStartup", Boolean.toString(ret));
                ret = notShutdown.isSelected();
                Program.JNProperty.setProperty("config.notShutdown", Boolean.toString(ret));
                setVisible(false);
                dispose();
            }
        });
        cancelButton = new JButton(resourceManager.getString("cancel"));
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                dispose();
            }
        }
        );
        buttonPane  = ButtonBarFactory.buildRightAlignedBar(okButton, cancelButton);
        buttonPane.setBorder(BorderFactory.createEmptyBorder(6,6,6,12));
        return buttonPane;
    }

    public void createLookAndFeelItem(){

    }
    public JPanel createGeneralPane(){
        DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout("3dlu, left:pref, 3dlu, pref, fill:pref:grow, 3dlu",
                "pref,1dlu, pref, 1dlu, pref, 1dlu, pref,1dlu, pref,1dlu, pref"));
        CellConstraints cc = new CellConstraints();
        builder.add(new JLabel(resourceManager.getString("laf")), cc.xy(2, 1));
        builder.add( lookAndFeelCombobox, cc.xy(4, 1));
        builder.add(new JLabel(resourceManager.getString("language")), cc.xy(2, 3));
        builder.add(languageCombobox, cc.xy(4, 3));
        useDecoratedChk = new JCheckBox(resourceManager.getString("useDecoratedFrames"));
        boolean ret = Boolean.parseBoolean(Program.JNProperty.getProperty("config.isDefaultDecorated"));
        useDecoratedChk.setSelected(ret);
        ret = Boolean.parseBoolean(Program.JNProperty.getProperty("config.miniToTray"));
        miniToTray = new JCheckBox(resourceManager.getString("miniToTray"));
        miniToTray.setSelected(ret);
        ret = Boolean.parseBoolean(Program.JNProperty.getProperty("config.runAtStartup"));
        runAtStartup = new JCheckBox(resourceManager.getString("runAtStartup"));
        runAtStartup.setSelected(ret);
        ret = Boolean.parseBoolean(Program.JNProperty.getProperty("config.notShutdown"));
        notShutdown = new JCheckBox(resourceManager.getString("notShutdown"));
        notShutdown.setSelected(ret);
        builder.add(useDecoratedChk, cc.xyw(2,5 , 3));
        builder.add(miniToTray, cc.xyw(2, 7, 3));
        builder.add(runAtStartup, cc.xyw(2, 9, 3));
        builder.add(notShutdown, cc.xyw(2, 11, 3));
        builder.setBorder(new TitledBorder(resourceManager.getString("appearance")));
        return builder.getPanel();
    }
    public JPanel createShortcutsPane(){
        JButton desktopButton = new JButton(resourceManager.getString("desktop"));
        desktopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        JButton startMenuButton = new JButton(resourceManager.getString("startMenu"));
        JButton quickLaunchButton = new JButton(resourceManager.getString("quickLaunch"));
        JButton startupButton = new JButton(resourceManager.getString("startup"));
        FormLayout layout  = new FormLayout("3dlu, pref, 3dlu", "3dlu, pref, 5dlu, pref, 5dlu, pref, 5dlu, pref, 5dlu");
        PanelBuilder builder = new PanelBuilder(layout);
        CellConstraints cc = new CellConstraints();
        builder.add(desktopButton, cc.xy(2, 2));
        builder.add(startMenuButton, cc.xy(2, 4));
        builder.add(quickLaunchButton, cc.xy(2, 6));
        builder.add(startupButton, cc.xy(2, 8));
        builder.setBorder(new TitledBorder(resourceManager.getString("shortcut")));
        return builder.getPanel();
    }


    class LookAndFeelComboboxRenderer implements ListCellRenderer{
        protected SubstanceDefaultListCellRenderer defaultRenderer = new SubstanceDefaultListCellRenderer();
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(value instanceof UIManager.LookAndFeelInfo){
                renderer.setText("Core - " + ((UIManager.LookAndFeelInfo)value).getName());
            } else if (value instanceof SkinInfo){
                renderer.setText("Substance - " + ((SkinInfo)value).getDisplayName());
            }
            return renderer;
        }
    }
    class LanguageComboboxRenderer implements ListCellRenderer{
        protected SubstanceDefaultListCellRenderer defaultRenderer = new SubstanceDefaultListCellRenderer();
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            renderer.setText(resourceManager.getString("language" + value.toString() + ".text"));
            String imageString = resourceManager.getString("language" + value.toString() + ".flag");
            renderer.setIcon(resourceManager.createImageIcon(imageString, resourceManager.getString("language" + value.toString() + ".text")));
            return renderer;
        }
    }



}
